package com.upcode.weighing.common.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

public class RequestTokenUtil {
    private static final Logger logger = LoggerFactory.getLogger(RequestTokenUtil.class);

    public static String getToken(HttpServletRequest request){
        String resultToken = null;
        String token = request.getHeader("Authorization");
        if(StringUtils.isNotEmpty(token)){
            boolean b = token.startsWith("B");
            if(b){
                resultToken = token.split("Bearer")[1].trim();
            }else{
                resultToken = token.split("bearer")[1].trim();
            }
        }else{
            resultToken = request.getParameter("access_token");
        }
        logger.info("Get the token from request header, token = "+resultToken);
        return resultToken;
    }

}
